﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;

namespace YJKGridTopologyCal.Geometries.Utilities
{
	public class GeometryMapper
	{
		public static IGeometry Map(IGeometry geom, Func<IGeometry, IGeometry> op)
		{
			List<IGeometry> list = new List<IGeometry>();
			for (int i = 0; i < geom.NumGeometries; i++)
			{
				IGeometry geometry = op(geom);
				if (geometry != null)
				{
					list.Add(geometry);
				}
			}
			return geom.Factory.BuildGeometry(list);
		}

		public static IGeometry Map(IGeometry geom, GeometryMapper.IMapOp op)
		{
			List<IGeometry> list = new List<IGeometry>();
			for (int i = 0; i < geom.NumGeometries; i++)
			{
				IGeometry geometry = op.Map(geom.GetGeometryN(i));
				if (geometry != null)
				{
					list.Add(geometry);
				}
			}
			return geom.Factory.BuildGeometry(list);
		}

		public static ICollection<IGeometry> Map(ICollection<IGeometry> geoms, GeometryMapper.IMapOp op)
		{
			List<IGeometry> list = new List<IGeometry>();
			foreach (IGeometry g in geoms)
			{
				IGeometry geometry = op.Map(g);
				if (geometry != null)
				{
					list.Add(geometry);
				}
			}
			return list;
		}

		public interface IMapOp
		{
			IGeometry Map(IGeometry g);
		}
	}
}
